<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/all.conf.php');
require_once(__PHY_WEB.__DIR_ADM.'/admin.inc.php');
require_once(__PHY_LIB.'/class/uWork.class.php');
require_once(__PHY_LIB.'/class/uWorkSize.class.php');
require_once(__PHY_LIB.'/class/uWorkSizeKuang.class.php');
require_once(__PHY_LIB.'/class/uWorkAlbum.class.php');
require_once(__PHY_LIB.'/class/uWorkSku.class.php');
require_once(__PHY_LIB.'/class/uUser.class.php');
require_once(__PHY_LIB.'/class/uCategory.class.php');

$rReq = validate($_REQUEST, 'msg:u|action:e[list,new,edit,size,update,insert,delete'
	.',add_album,update_album,delete_album,update_size,add_size,move_size,delete_size]');
$sMsg = $rReq['msg'];
$sAction = $rReq['action'];

if ('insert' == $sAction) {
	do {
		$rP = validate($_POST, 'category_id:n|has_size:n|owner_id:n|title:u|title_en:u|description:u|description_en:u|tags:u|year:n|size:u|size_en:u|weight:u|weight_en:u|price:f|price_en:f|stock_max:n');
		$rP['stock'] = $rP['stock_max'];
		$oOne = new uWork();
		foreach ($rP as $sField => $xValue) {
			$oOne->set($sField, $xValue);
		}
		$oOne->set('time_added', date('Y-m-d H:i:s'));
		$oOne->set('time_updated', date('Y-m-d H:i:s'));
		if ($s = $oOne->invalid()) {
			$sMsg .= "参数不规范: {$s}".$_br;
		}
		elseif ($oOne->insert()) {
			$oOne->setSku(0, 0, $rP);
			if ($_FILES['thumb']['tmp_name']) {
				$oOne->saveThumb($_FILES['thumb']['tmp_name']);
				$oOne->update();
			}
			$sMsg .= '录入成功。'.$_br;
		}
		else {
			$sMsg .= '录入失败'.$_br;
		}
	} while (false);
	$sAction = 'list';
}
elseif ('update' == $sAction) {
	do {
		$rP = validate($_POST, 'id:n|category_id:n|has_size:n|title:u|title_en:u|description:u|description_en:u|tags:u|year:n|size:u|size_en:u|weight:u|weight_en:u|price:f|price_en:f|stock:n|stock_max:n');
		$oOne = new uWork($rP['id']);
		foreach ($rP as $sField => $xValue) {
			$oOne->set($sField, $xValue);
		}
		if (!(0 < $_POST['id'])) {
			$sMsg .= "没有ID无法修改。".$_br;
		}
		elseif ($s = $oOne->invalid()) {
			$sMsg .= "参数不规范: {$s}".$_br;
		}
		elseif ($oOne->update()) {
			$oOne->setSku(0, 0, $rP);
			if ($_FILES['thumb']['tmp_name']) {
				$oOne->saveThumb($_FILES['thumb']['tmp_name']);
				$oOne->update();
			}
			$sMsg .= '修改成功。'.$_br;
		}
		else {
			$sMsg .= '修改失败'.$_br;
		}
	} while (false);
	$sAction = 'list';
}
elseif ('delete' == $sAction) {
	do {
		$rP = validate($_REQUEST, 'id:n');
		if (!$rP['id']) { $sMsg .= '无效'; break; }
		$oOne = new uWork($rP['id']);
		if ($oOne->delete()) {
			$sMsg .= '成功'; break;
		}
		else { $sMsg .= '失败'; break; }
	} while (false);
	$sAction = 'list';
}
elseif ('add_album' == $sAction) {
	do {
		$rP = validate($_POST, 'work:n|title:u|title_en:u');
		$rP['thumb_file'] = $_FILES['thumb']['tmp_name'];
		if (!$rP['work'] || !$rP['title'] || !$rP['thumb_file']) { $sMsg .= '无效'; break; }
		$oAlbum = new uWorkAlbum();
		$oAlbum->set('work_id', $rP['work']);
		$oAlbum->set('title', $rP['title']);
		$oAlbum->set('title_en', $rP['title_en']);
		$oAlbum->set('time_added', date('Y-m-d H:i:s'));
		$oAlbum->set('rank', $oAlbum->calcNewRank());
		if ($oAlbum->insert()) {
			$oAlbum->saveThumb($rP['thumb_file']);
			$oAlbum->update();
			$sMsg .= '成功'; break;
		}
		else { $sMsg .= '失败'; break; }
	} while (false);
	$_REQUEST['id'] = $_REQUEST['work'];
	$sAction = 'edit';
}
elseif ('update_album' == $sAction) {
	do {
		$rP = validate($_POST, 'id:n|work:n|rank:n|title:u|title_en:u');
		if (!$rP['id'] || !$rP['work'] || !$rP['rank'] || !$rP['title']) { $sMsg .= '无效'; break; }
		$oAlbum = new uWorkAlbum($rP['id']);
		$oAlbum->set('work_id', $rP['work']);
		$oAlbum->set('title', $rP['title']);
		$oAlbum->set('title_en', $rP['title_en']);
		$oAlbum->rankTo($rP['rank']);
		if ($oAlbum->update()) {
			if ($_FILES['thumb']['tmp_name']) {
				$oAlbum->saveThumb($_FILES['thumb']['tmp_name']);
				$oAlbum->update();
			}
			$sMsg .= '成功'; break;
		}
		else { $sMsg .= '失败'; break; }
	} while (false);
	$_REQUEST['id'] = $_REQUEST['work'];
	$sAction = 'edit';
}
elseif ('delete_album' == $sAction) {
	do {
		$rP = validate($_REQUEST, 'id:n|work:n');
		if (!$rP['work'] || !$rP['id']) { $sMsg .= '无效'; break; }
		$oAlbum = new uWorkAlbum($rP['id']);
		if ($oAlbum->delete()) {
			$sMsg .= '成功'; break;
		}
		else { $sMsg .= '失败'; break; }
	} while (false);
	$_REQUEST['id'] = $_REQUEST['work'];
	$sAction = 'edit';
}
elseif ('update_size' == $sAction) {
	do {
		$rP = validate($_POST, 'work_id:N|size_id:N|k_id:n|title:U|title_en:U|n_price:n|n_price_en:n|n_stock:n|n_stock_max:n|k_price:n|k_price_en:n|k_stock:n|k_stock_max:n');
		if (!$rP) { $sMsg .= '参数错误'.$_br; break; }
		$oSize = new uWorkSize($_POST['size_id']);
		$oSize->set('title', $rP['title']);
		$oSize->set('title_en', $rP['title_en']);
		if ($s = $oSize->invalid()) {
			$sMsg .= "参数不规范: {$s}".$_br;
		}
		elseif ($oSize->update()) {
			/// 无框sku ///
			if (0 < $rP['n_price'] && 0 < $rP['n_price_en']) {
				$oSku = new uWorkSku();
				$oSku->set('work_id', $rP['work_id']);
				$oSku->set('size_id', $oSize->getID());
				$oSku->set('kuang_id', 0);
				$oSku->create('work_id,size_id,kuang_id');
				$oSku->set('price', $rP['n_price']);
				$oSku->set('price_en', $rP['n_price_en']);
				$oSku->set('stock', $rP['n_stock']);
				$oSku->set('stock_max', $rP['n_stock_max']);
				$oSku->update();
			}
			/// 带框sku ///
			if (0 < $rP['k_price'] && 0 < $rP['k_price_en']) {
				if (0 < $rP['k_id']) {
					$oKuang = new uWorkSizeKuang($rP['k_id']);
				}
				else {
					$oKuang = new uWorkSizeKuang();
					$oKuang->set('work_id', $rP['work_id']);
					$oKuang->set('size_id', $oSize->getID());
					$oKuang->create('work_id,size_id');
				}
				$oSku = new uWorkSku();
				$oSku->set('work_id', $rP['work_id']);
				$oSku->set('size_id', $oSize->getID());
				$oSku->set('kuang_id', $oKuang->getID());
				$oSku->create('work_id,size_id,kuang_id');
				$oSku->set('price', $rP['k_price']);
				$oSku->set('price_en', $rP['k_price_en']);
				$oSku->set('stock', $rP['k_stock']);
				$oSku->set('stock_max', $rP['k_stock_max']);
				$oSku->update();
			}
			$sMsg .= '修改尺寸成功'.$_br;
		}
		else {
			$sMsg .= '修改尺寸失败'.$_br;
		}
	} while (false);
	$sAction = 'size';
}
elseif ('add_size' == $sAction) {
	do {
		$rP = validate($_POST, 'work_id:N|title:U|title_en:U|n_price:n|n_price_en:n|n_stock:n|n_stock_max:n|k_price:n|k_price_en:n|k_stock:n|k_stock_max:n');
		if (!$rP) { $sMsg .= '参数错误'.$_br; break; }
		$oSize = new uWorkSize();
		$oSize->set('work_id', $rP['work_id']);
		$oSize->set('rank', $oSize->calcNewRank());
		$oSize->set('title', $rP['title']);
		$oSize->set('title_en', $rP['title_en']);
		if ($s = $oSize->invalid()) {
			$sMsg .= "参数不规范: {$s}".$_br;
		}
		elseif ($oSize->insert()) {
			/// 无框sku ///
			if (0 < $rP['n_price'] && 0 < $rP['n_price_en']) {
				$oSku = new uWorkSku();
				$oSku->set('work_id', $rP['work_id']);
				$oSku->set('size_id', $oSize->getID());
				$oSku->set('kuang_id', 0);
				$oSku->create('work_id,size_id,kuang_id');
				$oSku->set('price', $rP['n_price']);
				$oSku->set('price_en', $rP['n_price_en']);
				$oSku->set('stock', $rP['n_stock']);
				$oSku->set('stock_max', $rP['n_stock_max']);
				$oSku->update();
			}
			/// 带框sku ///
			if (0 < $rP['k_price'] && 0 < $rP['k_price_en']) {
				$oKuang = new uWorkSizeKuang();
				$oKuang->set('work_id', $rP['work_id']);
				$oKuang->set('size_id', $oSize->getID());
				$oKuang->create('work_id,size_id');
				$oSku = new uWorkSku();
				$oSku->set('work_id', $rP['work_id']);
				$oSku->set('size_id', $oSize->getID());
				$oSku->set('kuang_id', $oKuang->getID());
				$oSku->create('work_id,size_id,kuang_id');
				$oSku->set('price', $rP['k_price']);
				$oSku->set('price_en', $rP['k_price_en']);
				$oSku->set('stock', $rP['k_stock']);
				$oSku->set('stock_max', $rP['k_stock_max']);
				$oSku->update();
			}
			$sMsg .= '增加尺寸成功'.$_br;
		}
		else {
			$sMsg .= '增加尺寸失败'.$_br;
		}
	} while (false);
	$sAction = 'size';
}
elseif ('delete_size' == $sAction) {
	do {
		$rP = validate($_GET, 'size_id:N');
		if (!$rP) { $sMsg .= '参数错误'.$_br; break; }
		$oSize = new uWorkSize($rP['size_id']);
		if ($oSize->delete()) {
			$sMsg .= '删除尺寸成功'.$_br;
		}
		else {
			$sMsg .= '删除尺寸失败'.$_br;
		}
	} while (false);
	$sAction = 'size';
}
elseif ('move_size' == $sAction) {
	do {
		$rP = validate($_GET, 'size_id:N|rank:N');
		if (!$rP) { $sMsg .= '参数错误'.$_br; break; }
		$oSize = new uWorkSize($rP['size_id']);
		if ($oSize->rankTo($rP['rank'])) {
			$sMsg .= '调序成功'.$_br;
		}
		else {
			$sMsg .= '调序失败'.$_br;
		}
	} while (false);
	$sAction = 'size';
}

if ('new' == $sAction) {
}
elseif ('edit' == $sAction) {
	$oOne = new uWork($_REQUEST['id']);
	$oOne->getSku(0, 0, true);
	$oUser = new uUser($oOne->get('owner_id'), 'nick');
	$oOne->set('owner_nick', $oUser->get('nick'));
	$oCtgry = new uCategory($oOne->get('category_id'));
	$oOne->set('BreadCrumb', $oCtgry->fetchBreadCrumb());
	$oTpl->assign('rOne', $oOne->getData());
	/// 图集 ///
	$oAlbumList = new uWorkAlbumList();
	$oAlbumList->cfgWhere("work_id='".$oOne->getID()."'");
	$oAlbumList->cfgOrder("rank");
	$oAlbumList->load();
	$nMaxRank = 0;
	while ($oAlbum = $oAlbumList->nextItem()) {
		$nMaxRank = max($nMaxRank, $oAlbum->get('rank'));
	}
	$oTpl->assign('nAlbumsMaxRank', $nMaxRank);
	$oTpl->assign('rAlbums', $oAlbumList->fetchArray());
}
elseif ('size' == $sAction) {
	$oOne = new uWork($_REQUEST['id']);
	$oTpl->assign('rOne', $oOne->getData());
	$oList = new uWorkSizeList();
	$oList->cfgWhere("work_id='".$oOne->getID()."'");
	$oList->cfgOrder("rank");
	$oList->load();
	$nRankMax = 0;
	while ($oSize = $oList->nextItem()) {
		$nRankMax = max($nRankMax, $oSize->get('rank'));
		/// 无框数据 ///
		$rSku = $oOne->getSku($oSize->getID(), 0);
		$oSize->set('n_price', $rSku['price']);
		$oSize->set('n_price_en', $rSku['price_en']);
		$oSize->set('n_stock', $rSku['stock']);
		$oSize->set('n_stock_max', $rSku['stock_max']);
		/// 有框数据 ///
		$oKuang = new uWorkSizeKuang();
		$oKuang->loadUnique(array(
			'work_id' => $oOne->getID(),
			'size_id' => $oSize->getID(),
		));
		if (0 < $oKuang->getID()) {
			$rSku = $oOne->getSku($oSize->getID(), $oKuang->getID());
			$oSize->set('k_id', $oKuang->getID());
			$oSize->set('k_price', $rSku['price']);
			$oSize->set('k_price_en', $rSku['price_en']);
			$oSize->set('k_stock', $rSku['stock']);
			$oSize->set('k_stock_max', $rSku['stock_max']);
		}
	}
	$oTpl->assign('rList', $oList->fetchArray());
	$oTpl->assign('nRankMax', $nRankMax);
}
elseif ('list' == $sAction) {
	$rP = validate($_GET, 'category:n|pg:n');
	$oTpl->assign('rP', $rP);
	$sWhere = "TRUE";
	/// 按类别查找 ///
	$oCtgry = new uCategory($rP['category']);
	if (0 < $oCtgry->getID()) {
		$sWhere .= " AND category_id IN ('".implode("','", $oCtgry->getTreeIdsByTop($oCtgry->getID()))."')";
	}
	/// 作品列表 ///
	$rPager = array(
		'Sze' => 25,
		'Req' => 'pg',
		'Num' => max(1, $rP['pg']),
		'Bse' => http_build_query(array_slice($rP, 0, -1)),
	);
	$oList = new uWorkList();
	$oList->cfgWhere($sWhere);
	$rPager['Cnt'] = $oList->countAll();
	$rPager['Max'] = ceil($rPager['Cnt']/$rPager['Sze']);
	$oList->cfgOrder("id DESC");
	$oList->load($rPager['Sze']*($rPager['Num'] - 1), $rPager['Sze']);
	while ($oOne = $oList->nextItem()) {
		$oUser = new uUser($oOne->get('owner_id'), 'nick');
		$oOne->set('owner_nick', $oUser->get('nick'));
		$oCtgry = new uCategory($oOne->get('category_id'));
		$oOne->set('BreadCrumb', $oCtgry->fetchBreadCrumb());
	}
	$oTpl->assign('rList', $oList->fetchArray());
	$oTpl->assign('rPager', $rPager);
}

/// 类别目录列表 ///
$oCtgryList = new uCategoryList();
$oCtgryList->cfgOrder("tree");
$oCtgryList->load();
while ($oCtgry = $oCtgryList->nextItem()) {
	$sBreadCrumb = '';
	foreach ($oCtgry->fetchBreadCrumb() as $r) {
		$sBreadCrumb .= $r['title'].' > ';
	}
	$oCtgry->set('BreadCrumb', substr($sBreadCrumb, 0, -3));
}
$oTpl->assign('rSlctCtgrys', $oCtgryList->fetchArray());

/// 艺术家列表 ///
$oArtistList = new uUserList();
$oArtistList->cfgWhere("role_artist=1");
$oArtistList->cfgOrder("id");
$oArtistList->load();
while ($oArtist = $oArtistList->nextItem()) {
	;
}
$oTpl->assign('rSlctArtists', $oArtistList->fetchArray());

$oTpl->assign('sMsg', $sMsg);
$oTpl->assign('sAction', $sAction);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
